﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestTracking.Helper;
using TestTracking.Model;

namespace TestTracking.Common
{
    public class BusinessProcess
    {
        static SqlHelp sqlHelper = new SqlHelp();
        /// <summary>
        /// 查询互调详细信息
        /// </summary>
        /// <param name="tEntity">Log实体</param>
        /// <param name="dt">测试基础表</param>
        /// <returns></returns>
        public static string QueryTestLogs(TrackingModel tModel, out DataTable dt)
        {
            string retMsg = string.Empty;//数据库返回的状态信息
            SqlParameter[] sqlParameters = null;
            try
            {
                sqlParameters = new SqlParameter[] {
                new SqlParameter("@QRCode",SqlDbType.NVarChar,50),
                new SqlParameter("@Station",SqlDbType.NVarChar,50),
                new SqlParameter("@Sequence",SqlDbType.NVarChar,50),
                new SqlParameter("@Result",SqlDbType.NVarChar,50),
                new SqlParameter("@StartTime",SqlDbType.NVarChar,50),
                new SqlParameter("@StopTime",SqlDbType.NVarChar,50),
                new SqlParameter("@StatusCode",SqlDbType.NVarChar,10),
                new SqlParameter("@StatusMsg",SqlDbType.NVarChar,1000)
                };
                sqlParameters[0].Value = tModel.QR ?? "";
                sqlParameters[1].Value = tModel.Station ?? "1";
                sqlParameters[2].Value = tModel.Status =="ALL"?"*":"1";
                sqlParameters[3].Value = tModel.Result ?? "1";
                sqlParameters[4].Value = tModel.StartTime ??"";
                sqlParameters[5].Value = tModel.StopTime ?? "";
                sqlParameters[6].Direction = ParameterDirection.Output;
                sqlParameters[7].Direction = ParameterDirection.Output;
                retMsg = sqlHelper.ExecuteStoredProcedureAndReturnDatatable("procQueryG02TestLog", sqlParameters, out dt);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return retMsg;
        }
        /// <summary>
        /// 杀掉进程
        /// </summary>
        /// <param name="processName">进程名</param>
        public static void KillProcess(string processName)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (p.ProcessName.Contains(processName))
                {
                    try
                    {
                        p.Kill();
                        p.WaitForExit(); // possibly with a timeout 
                    }
                    catch (Win32Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                    catch (InvalidOperationException e)
                    {
                        throw new Exception(e.Message);
                    }
                }

            }
        }
    }
}
